﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Com.Framework.Data;

namespace Com.Library.DB.User
{
	public class UserModel
	{
		public static ListData<UserEntity, OutputEntity> GetList(byte IsRemoved, int PageNo, byte PageSize, string IsLocalUser, string UserID_search, string UserName_search, byte? GroupType_search, DateTime? JoinDate_start, DateTime? JoinDate_end)
		{
			UserGetListArguments arg = new UserGetListArguments();
			arg.PageNo = PageNo;
			arg.PageSize = PageSize;
			if (IsLocalUser != string.Empty)
				arg.IsLocalUser_search = IsLocalUser;
			if (UserID_search != null && UserID_search != string.Empty)
				arg.strUserID_search = UserID_search;
			if (UserName_search != null && UserName_search != string.Empty)
				arg.strUserName_search = UserName_search;
			if (GroupType_search.HasValue)
				arg.n1GroupType_search = GroupType_search.Value;
			if (JoinDate_start.HasValue)
				arg.dtJoinDate_start_search = JoinDate_start.Value;
			if (JoinDate_end.HasValue)
				arg.dtJoinDate_end_search = JoinDate_end.Value;
			arg.IsRemoved = IsRemoved;
			UserGetList sp = new UserGetList();
			sp.SetArguments(arg);
			List<UserEntity> list = sp.Execute();
			OutputEntity output = sp.GetOutput();

			ListData<UserEntity, OutputEntity> result = new ListData<UserEntity, OutputEntity>(list, output);

			return result;
		}

		public static byte UserLogin(string UserID, string UserPWD, string LoginIP)
		{
			UserLoginArguments arg = new UserLoginArguments();
			arg.strUserID = UserID;
			arg.strMD5PWD = Com.Framework.Enc.Crypt.MD5HashCrypt(UserPWD);
			arg.strLoginIP = LoginIP;

			UserLogin sp = new UserLogin();
			sp.SetArguments(arg);
			sp.ExecuteNonQuery();
			DataEntity output = sp.GetOutput();

			byte LoginErrorType = output.GetData2("n1LoginErrorType", (byte)0);
			return LoginErrorType;
		}

		public static UserEntity UserInfo(string UserID)
		{
			UserGetInfoArguments arg = new UserGetInfoArguments();
			arg.strUserID = UserID;
			UserGetInfo sp = new UserGetInfo();
			sp.SetArguments(arg);
			sp.ExecuteNonQuery();
			UserEntity output = sp.GetOutput();

			return output;
		}

		public static UserEntity UserInfo(int UserNo)
		{
			UserGetInfoArguments arg = new UserGetInfoArguments();
			arg.n4UserNo = UserNo;
			UserGetInfo sp = new UserGetInfo();
			sp.SetArguments(arg);
			sp.ExecuteNonQuery();
			UserEntity output = sp.GetOutput();

			return output;
		}

		public static void Remove(int UserNo)
		{
			UserRemoveArguments arg = new UserRemoveArguments();
			arg.n4UserNo = UserNo;
			UserRemove sp = new UserRemove();
			sp.SetArguments(arg);
			sp.ExecuteNonQuery();
		}

		public static void Restore(int UserNo)
		{
			UserRestoreArguments arg = new UserRestoreArguments();
			arg.n4UserNo = UserNo;
			UserRestore sp = new UserRestore();
			sp.SetArguments(arg);
			sp.ExecuteNonQuery();
		}

		public static void ChangePWD(int UserNo, string UserPWD)
		{
			UserModifyArguments arg = new UserModifyArguments();
			arg.n4UserNo = UserNo;
			arg.strMD5PWD = Com.Framework.Enc.Crypt.MD5HashCrypt(UserPWD);
			UserModify sp = new UserModify();
			sp.SetArguments(arg);
			sp.ExecuteNonQuery();
		}

		public static void Create(UserCreateArguments arg)
		{
			UserCreate sp = new UserCreate();
			sp.SetArguments(arg);
			sp.ExecuteNonQuery();
		}

		public static void Modify(UserModifyArguments arg)
		{
			UserModify sp = new UserModify();
			sp.SetArguments(arg);
			sp.ExecuteNonQuery();
		}
	}
}
